﻿using System;
using System.Collections.Generic;
using Character;
using UnityEngine;

namespace H
{
	// Token: 0x02000263 RID: 611
	internal class H_State_Loop : H_State
	{
		// Token: 0x06001015 RID: 4117 RVA: 0x00067238 File Offset: 0x00065438
		public H_State_Loop(H_Members members) : base(members)
		{
		}

		// Token: 0x06001016 RID: 4118 RVA: 0x000672A8 File Offset: 0x000654A8
		public override void In(H_State.Message msg)
		{
			float blend = H_State.BlendMessage.GetBlend(msg, 0.2f);
			this.members.param.anyAction = true;
			this.members.PlayAnime("Loop", blend);
			this.members.param.mouth = this.members.StyleData.GetInsertedMouth();
			this.members.EnableTinIK = true;
			this.members.VoiceExpression(H_Voice.TYPE.ACT);
			this.members.MaleExpression(H_Expression_Male.TYPE.NORMAL);
			this.seTimer = -1f;
			this.In_Visitor();
			this.expressionTimers_F = new float[this.members.GetFemales().Count];
			for (int i = 0; i < this.expressionTimers_F.Length; i++)
			{
				this.expressionTimers_F[i] = this.NextExpressionTime();
			}
			this.expressionTimers_M = new float[this.members.GetMales().Count];
			for (int j = 0; j < this.expressionTimers_M.Length; j++)
			{
				this.expressionTimers_M[j] = this.NextExpressionTime();
			}
			this.CheckLostVirgin();
		}

		// Token: 0x06001017 RID: 4119 RVA: 0x000673C4 File Offset: 0x000655C4
		private void CheckLostVirgin()
		{
			H_StyleData styleData = this.members.StyleData;
			bool flag = false;
			if (styleData.type == H_StyleData.TYPE.INSERT)
			{
				flag = true;
			}
			else if (styleData.type == H_StyleData.TYPE.PETTING && (styleData.detailFlag & 1024) != 0)
			{
				flag = true;
			}
			if (flag)
			{
				List<Female> females = this.members.GetFemales();
				bool flag2 = (this.members.StyleData.detailFlag & 16) != 0;
				bool flag3 = (this.members.StyleData.detailFlag & 32) != 0;
				for (int i = 0; i < females.Count; i++)
				{
					if (flag2)
					{
						females[i].personality.insertVagina = true;
						if (females[i].personality.vaginaVirgin)
						{
							females[i].personality.LostVaginaVirgin();
							females[i].SetVirginBlood(true);
						}
					}
					if (flag3)
					{
						females[i].personality.insertAnal = true;
						if (females[i].personality.analVirgin)
						{
							females[i].personality.LostAnalVirgin();
						}
					}
				}
			}
		}

		// Token: 0x06001018 RID: 4120 RVA: 0x00067508 File Offset: 0x00065708
		private float NextExpressionTime()
		{
			return UnityEngine.Random.Range(this.minExpressionTime, this.maxExpressionTime);
		}

		// Token: 0x06001019 RID: 4121 RVA: 0x0006751C File Offset: 0x0006571C
		private void In_Visitor()
		{
			if (this.members.h_scene.visitor != null && this.members.h_scene.visitor.GetHuman().sex == SEX.FEMALE)
			{
				int num = UnityEngine.Random.Range(0, this.visitorVoiceProbability);
				this.visitorVoicePlay = (num == 0);
				if (this.visitorVoicePlay)
				{
					this.visitorVoiceTimer = UnityEngine.Random.Range(this.visitorVoiceMin, this.visitorVoiceMax);
				}
			}
		}

		// Token: 0x0600101A RID: 4122 RVA: 0x00067598 File Offset: 0x00065798
		public override void Main()
		{
			float num = this.members.input.Speed;
			num = ((num <= 0f) ? Mathf.Lerp(1f, this.minSpeed, -num) : Mathf.Lerp(1f, this.maxSpeed, num));
			float pose = this.members.input.Pose;
			float stroke = this.members.input.Stroke;
			this.members.SetLoopPose(pose);
			this.members.SetLoopStroke(stroke);
			this.members.SetSpeed(num);
			H_StyleData styleData = this.members.StyleData;
			bool addFemale = styleData.type == H_StyleData.TYPE.INSERT || styleData.type == H_StyleData.TYPE.PETTING;
			bool addMale = styleData.type == H_StyleData.TYPE.INSERT || styleData.type == H_StyleData.TYPE.SERVICE;
			this.members.AddGage(addFemale, addMale);
			if (this.members.FemaleGageVal >= 1f)
			{
				base.ChangeState(H_STATE.SPURT, new H_State_Spurt.SpurtMsg(XTC_TYPE.XTC_F));
				return;
			}
			bool forced = false;
			if (this.members.param.speed != H_SPEED.NORMAL && this.checkSpeed != this.members.param.speed)
			{
				forced = true;
				this.checkSpeed = this.members.param.speed;
			}
			this.UpdateVoice(forced);
			this.UpdateSE(num);
			this.UpdateVisitor();
		}

		// Token: 0x0600101B RID: 4123 RVA: 0x00067708 File Offset: 0x00065908
		private void UpdateVoice(bool forced = false)
		{
			List<Female> females = this.members.GetFemales();
			for (int i = 0; i < females.Count; i++)
			{
				if (!females[i].IsVoicePlaying() || forced)
				{
					this.members.VoiceExpression(i, H_Voice.TYPE.ACT);
					this.expressionTimers_F[i] = this.NextExpressionTime();
				}
			}
			for (int j = 0; j < this.expressionTimers_F.Length; j++)
			{
				if (this.expressionTimers_F[j] <= 0f)
				{
					H_Expression.TYPE type = H_Members.VoiceToExpressionType_Act(this.members.param, H_Voice.KIND.PANT);
					this.members.Expression(type);
					this.expressionTimers_F[j] = this.NextExpressionTime();
				}
				else
				{
					this.expressionTimers_F[j] -= Time.deltaTime;
				}
			}
			for (int k = 0; k < this.expressionTimers_M.Length; k++)
			{
				if (this.expressionTimers_M[k] <= 0f)
				{
					this.members.MaleExpression(H_Expression_Male.TYPE.NORMAL);
					this.expressionTimers_M[k] = this.NextExpressionTime();
				}
				else
				{
					this.expressionTimers_M[k] -= Time.deltaTime;
				}
			}
		}

		// Token: 0x0600101C RID: 4124 RVA: 0x00067844 File Offset: 0x00065A44
		private void UpdateSE(float speed)
		{
			Female female = this.members.GetFemale(0);
			float delay = this.members.h_scene.se.Delay;
			float num = female.body.Anime.GetCurrentAnimatorStateInfo(0).normalizedTime + delay;
			float num2 = this.seTimer;
			if (this.seTimer == -1f || num % 1f < num2 % 1f)
			{
				if (this.members.StyleData.type == H_StyleData.TYPE.INSERT)
				{
					this.members.h_scene.se.Play_Piston(female, speed);
				}
				else if (this.members.StyleData.type == H_StyleData.TYPE.PETTING && (this.members.StyleData.detailFlag & 16) != 0)
				{
					this.members.h_scene.se.Play_Crotch(female, speed);
				}
			}
			this.seTimer = num;
		}

		// Token: 0x0600101D RID: 4125 RVA: 0x0006793C File Offset: 0x00065B3C
		private void UpdateVisitor()
		{
			if (!this.visitorVoicePlay)
			{
				return;
			}
			this.visitorVoiceTimer -= Time.deltaTime;
			if (this.visitorVoiceTimer <= 0f)
			{
				this.members.h_scene.VisitorVoiceExpression(H_VisitorVoice.TYPE.ACT);
				this.visitorVoicePlay = false;
			}
		}

		// Token: 0x0600101E RID: 4126 RVA: 0x00067990 File Offset: 0x00065B90
		public override void Out()
		{
			this.members.SetSpeed(1f);
		}

		// Token: 0x0600101F RID: 4127 RVA: 0x000679A4 File Offset: 0x00065BA4
		public override void OnInput(H_INPUT input)
		{
			if (input == H_INPUT.EJA_IN)
			{
				base.ChangeState(H_STATE.SPURT, new H_State_Spurt.SpurtMsg(XTC_TYPE.EJA_IN));
			}
			else if (input == H_INPUT.EJA_OUT)
			{
				base.ChangeState(H_STATE.SPURT, new H_State_Spurt.SpurtMsg(XTC_TYPE.EJA_OUT));
			}
			else if (input == H_INPUT.XTC_F)
			{
				base.ChangeState(H_STATE.SPURT, new H_State_Spurt.SpurtMsg(XTC_TYPE.XTC_F));
			}
			else if (input == H_INPUT.XTC_W)
			{
				base.ChangeState(H_STATE.SPURT, new H_State_Spurt.SpurtMsg(XTC_TYPE.XTC_W));
			}
		}

		// Token: 0x04001163 RID: 4451
		private float maxSpeed = 4f;

		// Token: 0x04001164 RID: 4452
		private float minSpeed = 0.5f;

		// Token: 0x04001165 RID: 4453
		private float maxExpressionTime = 7.5f;

		// Token: 0x04001166 RID: 4454
		private float minExpressionTime = 2.5f;

		// Token: 0x04001167 RID: 4455
		private H_SPEED checkSpeed = H_SPEED.NORMAL;

		// Token: 0x04001168 RID: 4456
		private float seTimer = -1f;

		// Token: 0x04001169 RID: 4457
		private float visitorVoiceTimer;

		// Token: 0x0400116A RID: 4458
		private float visitorVoiceMin = 5f;

		// Token: 0x0400116B RID: 4459
		private float visitorVoiceMax = 15f;

		// Token: 0x0400116C RID: 4460
		private int visitorVoiceProbability = 3;

		// Token: 0x0400116D RID: 4461
		private bool visitorVoicePlay;

		// Token: 0x0400116E RID: 4462
		private float[] expressionTimers_F;

		// Token: 0x0400116F RID: 4463
		private float[] expressionTimers_M;
	}
}
